<%--
  Created by IntelliJ IDEA.
  User: Fmc
  Date: 2022/6/29
  Time: 9:29
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>修改用户个人信息</title>
        <link rel="stylesheet" href="../../static/layui-main/dist/css/layui.css">
        <script src="../../static/layui-main/src/layui.js"></script>
        <script src="../../static/jquery-3.5.0.min.js"></script>
    </head>
    <body>
        <div style="width: 650px;margin-top: 30px;">
            <form class="layui-form" action="" id="saveUserForm" lay-filter="saveUserFormFilter">
                <div class="layui-form-item">
                    <label class="layui-form-label">用户Id</label>
                    <div class="layui-input-inline">
                        <input type="text" name="userId" required lay-verify="required" readonly="readonly" placeholder="用户名id"
                               value="" class="layui-input" style="background-color: #eeeeee;">
                    </div>
                    <label class="layui-form-label">部门名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="deptId" disabled value="" class="layui-input" style="background-color: #eeeeee;">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">登录名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="loginName" value="" class="layui-input">
                    </div>
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="userName" required lay-verify="required" placeholder="用户名" value=""
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-inline">
                        <input type="text" name="email" value="" class="layui-input">
                    </div>
                    <label class="layui-form-label">电话</label>
                    <div class="layui-input-inline">
                        <input type="text" name="phone" value="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-inline">
                        <input type="text" name="sex" value="" class="layui-input">
                    </div>
                    <label class="layui-form-label">创建人</label>
                    <div class="layui-input-inline">
                        <input type="text" disabled name="createBy" value="" class="layui-input" style="background-color: #eeeeee;">
                    </div>
                </div>
                <input type="hidden" id="avatar2" name="avatar" value="">
                <div class="layui-form-item">
                    <label class="layui-form-label">头像</label>
                    <div class="layui-upload">
                        <img class="layui-upload-img" id="demo0" style="width: 100px;" src="">
                        </br>
                        <button type="button" class="layui-btn" id="test3" style="margin-left: 110px;">修改图片</button>
                        <div class="layui-upload-list" style="margin-left: 110px;">
                            <p id="demoText3"></p>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item" >
                    <div class="layui-input-inline" style="width: 100%;text-align: center">
                        <button class="layui-btn" lay-submit lay-filter="formSaveUser">保存</button>
                    </div>
                </div>
            </form>
        </div>
    </body>
    <script>
        layui.use(['upload','jquery','form'],function (){
            var upload = layui.upload;
            var $ = layui.jquery;
            var form = layui.form;

            $.ajax({
               url: '${pageContext.request.contextPath}/userinfo/selectById.do'
               ,data: ''
               ,dataType: 'json'
               ,type: 'post'
               ,success: function (redata) {
                    if (redata.code == 0) {
                        var deptId = redata.data.deptId;
                        // 回显用户信息
                        form.val('saveUserFormFilter', {
                            "userId": redata.data.userId
                            , "deptId": redata.data.dept.deptName
                            , "loginName": redata.data.loginName
                            , "userName": redata.data.userName
                            , "email": redata.data.email
                            , "phone": redata.data.phone
                            , "sex": redata.data.sex
                            , "createBy": redata.data.createBy
                        })
                        $('#demo0').attr('src', redata.data.avatar);
                    }
                }
            });
            //常规使用 - 普通图片上传
            upload.render({
                elem: '#test3'
                ,url: '${pageContext.request.contextPath}/userinfo/upload.do'
                ,field: 'xxx'
                ,done: function(res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    //上传成功的一些操作
                    $('#avatar3').val(res.data.src);
                    $('#demo0').attr('src', res.data.src); //图片链接（base64）
                },
                error: function() {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText3');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function() {
                        uploadInst.upload();
                    });
                }
            });
            // 保存修改后的信息
            form.on('submit(formSaveUser)', function (data) {
                $.ajax({
                    url: '${pageContext.request.contextPath}/userinfo/updateOneself.do'
                    , data: {
                        "userId": data.field.userId
                        ,"loginName": data.field.loginName
                        ,"userName": data.field.userName
                        ,"email": data.field.email
                        ,"phone": data.field.phone
                        ,"sex": data.field.sex
                        ,"avatar": data.field.avatar
                    }
                    , dataType: "json"
                    , type: "post"
                    , success: function (redata) {
                        if (redata.code == 0) {
                            layer.msg(redata.msg);
                        } else {
                            layer.msg(redata.msg);
                        }
                    }
                });
                return false;
            })

        })
    </script>
</html>
